<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加口令</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
	#ggtable .layui-input{ display:inline;height:30px}
	#ggtable .layui-btn{ margin-top:-3px;margin-left:1px}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-card layui-col-md12">
				<div class="layui-card-header">
					{if $info.id}编辑口令{else}添加口令{/if}
					<i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i>
				</div>
				<div class="layui-card-body" pad15>
					<!-- <blockquote class="layui-elem-quote"></blockquote> -->
					<div class="flex">
						<div class="layui-form flex1" lay-filter="" style="padding-bottom:20px">
							<input type="hidden" name="info[id]" value="{$info.id}"/>
							<input type="hidden" name="info[type]" value="{$info.type}"/>

							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>口令名称：</label>
								<div class="layui-input-inline" style="width:300px">
									<input type="text" class="layui-input" name="info[name]" value="{$info.name}" lay-verify="required" lay-verType="tips"/>
								</div>
							</div>

<!--							<div class="layui-form-item">-->
<!--								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>活动规则：</label>-->
<!--								<div class="layui-input-inline" style="width:300px">-->
<!--									<textarea class="layui-textarea" name="info[guize]" lay-verify="required" lay-verType="tips">{$info.guize}</textarea>-->
<!--								</div>-->
<!--							</div>-->
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>开始时间：</label>
								<div class="layui-input-inline">
									<input type="text" id="starttime" name="info[starttime]" value="{:date('Y-m-d H:i:s',$info['starttime'])}" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>结束时间：</label>
								<div class="layui-input-inline">
									<input type="text" id="endtime" name="info[endtime]" value="{:date('Y-m-d H:i:s',$info['endtime'])}" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>总奖品数：</label>
								<div class="layui-input-inline">
									<input type="text" class="layui-input" name="info[num]" value="{$info.num}" lay-verify="required" lay-verType="tips"/>
								</div>
								<div class="layui-form-mid layui-word-aux"></div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>每人参与的总次数：</label>
								<div class="layui-input-inline">
									<input type="text" class="layui-input" name="info[pertotal]" value="{$info.pertotal}" lay-verify="required" lay-verType="tips"/>
								</div>
								<div class="layui-form-mid layui-word-aux">0不限制</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>每人每天可参与次数：</label>
								<div class="layui-input-inline">
									<input type="text" class="layui-input" name="info[perday]" value="{$info.perday}" lay-verify="required" lay-verType="tips"/>
								</div>
								<div class="layui-form-mid layui-word-aux">0不限制</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:100px">赠送{:t('优惠券')}：</label>
								<div class="layui-input-inline" style="width:500px">
									<input type="radio" name="info[give_coupon]" value="0" {if $info['give_coupon']==0}checked{/if} title="关闭" lay-filter="give_coupon"/>
									<input type="radio" name="info[give_coupon]" value="1" {if $info['give_coupon']==1}checked{/if} title="开启" lay-filter="give_coupon"/>
								</div>
								<div class="give_coupon" style="clear:both;{if $info['give_coupon']!=1}display:none{/if}">
									<label class="layui-form-label" style="width:100px"></label>
									<div class="layui-input-inline" style="width:auto; overflow: hidden;">
										<table class="layui-table choose-coupon" style="width:500px" id="choose-coupon">
											<thead>
											<tr>
												<th>ID</th>
												<th>名称</th>
												<th>库存</th>
												<th>操作</th>
											</tr>
											</thead>
											{if $couponList}
											{foreach $couponList as $k=>$ff}
											<tr class="choose-tr-list"><td>{$ff.id}</td><td>{$ff.name}</td><td>{$ff.stock}</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delChooseTr(this,{$ff.id})">删除</button></td></tr>
											{/foreach}
											{/if}
											<tr class="choose-tr-add">
												<td colspan="4"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseCoupon()">添加</button></td>
											</tr>
										</table>
										<input type="hidden" name="info[coupon_ids]" value="{$info.coupon_ids}"/>
									</div>
									<div class="layui-form-mid layui-word-aux">请确认优惠券的有效期、库存有效</div>
								</div>

								<script>
									var chooseCouponLayer;
									function showChooseCoupon(){
										chooseCouponLayer = layer.open({type:2,title:'选择{:t(\'优惠券\')}',content:"{:url('Coupon/choosecoupon')}&type=all",area:['1000px','600px'],shadeClose:true});
									}
									function choosecoupon(res){
										var detail = res;
										var chooseClassName = '.choose-coupon';
										layer.close(chooseCouponLayer);
										var objIds = [];
										var isadd = 0;
										$(chooseClassName).find('.choose-tr-list').each(function(){
											var thisfid = $(this).find('td:eq(0)').html();
											if(thisfid == detail.id){
												isadd = 1
												dialog('该项已添加过了');
											}
											objIds.push(thisfid)
										})
										if(isadd == 0){
											objIds.push(detail.id)
											$("input[name='info[coupon_ids]']").val(objIds.join(','));
											var tr = '<tr class="choose-tr-list"><td>'+detail.id+'</td><td>'+detail.name+'</td><td>'+detail.stock+'</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delChooseTr(this,'+detail.id+')">删除</button></td></tr>';
											$(chooseClassName).find('.choose-tr-add').before(tr);
										}
										// $(chooseClassName).find('.choose-tr-add').hide();
									}
									function delChooseTr(obj,fid){
										$(obj).closest('.choose-tr-list').remove();
										var objIds = [];
										$('#choose-coupon').find('.choose-tr-list').each(function(){
											objIds.push($(this).find('td:eq(0)').html())
										})
										console.log(objIds)
										$("input[name='info[coupon_ids]']").val(objIds.join(','));
										// $(obj).closest('table').find('.choose-tr-add').show();
									}
								</script>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>参与条件：</label>
								<div class="layui-input-inline" style="width:500px">
									<input type="checkbox" name="info[gettj][]" value="-1" title="所有人" {if in_array('-1',$info['gettj'])}checked{/if}/>
									<input type="checkbox" name="info[gettj][]" value="0" title="关注用户" {if in_array('0',$info['gettj'])}checked{/if}/>
									{foreach $memberlevel as $v}
									<input type="checkbox" name="info[gettj][]" value="{$v.id}" title="{$v.name}" {if in_array($v['id'],$info['gettj'])}checked{/if}/>
									{/foreach}
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"><span class="redstar">*</span>状 态：</label>
								<div class="layui-input-inline">
									<input type="radio" name="info[status]" value="1" title="开启" {if !$info['id'] || $info['status']==1}checked{/if}/>
									<input type="radio" name="info[status]" value="0" title="关闭" {if $info['id'] && $info['status']==0}checked{/if}/>
								</div>
							</div>


							<div class="layui-form-item">
								<label class="layui-form-label" style="width:110px"></label>
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit lay-filter="formsubmit">提 交</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
		layui.form.on('radio(give_coupon)',function(data){
			if(data.value==1){
				$('.give_coupon').show();
			}else{
				$('.give_coupon').hide();
			}
		})
	// layui.form.on('radio(set_use_type)',function(data){
	// 	if(data.value=="1"){
	// 		$('#usescore').show();
	// 		$('#usemoney').hide();
	// 	}
	// 	if(data.value=="2"){
	// 		$('#usescore').hide();
	// 		$('#usemoney').show();
	// 	}
	//
	// });
	//日期时间选择器
	layui.laydate.render({
		elem: '#starttime',
		type: 'datetime',
		trigger: 'click'
	});
	layui.laydate.render({
		elem: '#endtime',
		type: 'datetime',
		trigger: 'click'
	});

	layui.form.on('submit(formsubmit)', function(obj){
		var field = obj.field
		//console.log(field);return;
		var index = layer.load();
		$.post("{:url('save')}",field,function(data){
			layer.close(index);
			dialog(data.msg,data.status);
			if(data.status == 1){
				setTimeout(function(){
					parent.layer.closeAll();
					parent.tableIns.reload()
				},1000)
			}
		})
	})


	</script>

	{include file="public/copyright"/}
</body>
</html>